/**
 * 
 */
package com.realgodo.site.entity.security;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.realgodo.site.entity.IdEntity;

@Entity(name = "User")
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries( {
		@NamedQuery(name = "findUserByName", query = "from User o where o.loginName = ?"),
		@NamedQuery(name = "findUserByEmail", query = "from User o where o.email = ?"),
		@NamedQuery(name = "findUserByCode", query = "from User o where o.activationCode = ?") })
public class User extends IdEntity {

	private static final long serialVersionUID = 908630874488041319L;

	/** 用户名 等同于Space.domain */
	private String loginName;
	private String password;

	/** 真是姓名 */
	private String name;
	/** 性别 sex = 1 男 sex = 0 女 */

	private int sex = 1;
	private Date birthday;
	private String email;
	private String tel;
	private String mobile;

	private Integer vipMark;
	/** 0:未认证 -1 :认证拒绝 1:认证通过 作废字段？ */
	private int stauts = STAUTS_UN_AUDIT;

	private int enable = 0;

	private String activationCode;

	private Date registDate = new Date();
	private Date activationDate;

	private Set<Role> roles = new LinkedHashSet<Role>(); // 有序的关联对象集合.

	private String avatar;

	private String weight;
	private String height;
	/** 学历 */
	private String schoolRecord;
	/** 培训 */
	private String training;
	/** 作品 */
	private String opus;
	private String qq;
	private String music;
	private String popStar;
	/** 身份证 */
	private String idcard;
	/** 职业 */
	private String job;
	/** 现居城市: */
	private String city;
	/** 任职机构/职位/在读学校 */
	private String school;

	/** MEIMEI大侠 */
	private int meiMeiMan = 0;
	/** MEIMEI会员 */
	private int meiMeiMember = 0;
	/** 首页推荐 */
	private int recommend = 0;
	/** 激活码 for email */
	private String activeCode;
	/** 访问人数 */
	private Long visitCount = 0L;

	/** Transient field 主要职业中文 */
	private String vocationChinese;
	/** Transient field 主要职业英文 */
	private String vocationEnglish;

	/** 认证通过 */
	public static final int STAUTS_AUTH = 1;
	/** 未认证 */
	public static final int STAUTS_UN_AUDIT = 0;
	/** 认证拒绝 */
	public static final int STAUTS_REJECT = -1;
	public static final int MEIMEI_MAN_ALL = -1;
	public static final int MEIMEI_MAN_YES = 1;
	public static final int MEIMEI_MAN_NO = 0;
	public static final int MEIMEI_MEMBER_ALL = -1;
	public static final int MEIMEI_MEMBER_YES = 1;
	public static final int MEIMEI_MEMBER_NO = 0;
	public static final int RECOMMEND_ALL = -1;
	public static final int RECOMMEND_YES = 1;
	public static final int RECOMMEND_NO = 0;

	public static final int ENABLED = 1;
	public static final int DISENABLED = 0;
	public static final int ENABLED_DISENABLED_ALL = -1;

	private String key;

	@Column(name = "login_name", length = 20)
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Column(length = 50)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "true_name", length = 20)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 50)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	// 避免定义CascadeType.REMOVE, 否则删除角色时会连带删除拥有它的用户.
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	// 多对多定义.
	@JoinTable(name = "USERS_ROLES", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	// Fecth定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象的id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Column(length = 2)
	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	@Temporal(TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Column(length = 18)
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Column(length = 12)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Integer getVipMark() {
		return vipMark;
	}

	public void setVipMark(Integer vipMark) {
		this.vipMark = vipMark;
	}

	@Column(name = "enable")
	public int getEnable() {
		return enable;
	}

	public void setEnable(int enable) {
		this.enable = enable;
	}

	@Column(name = "activation_code")
	public String getActivationCode() {
		return activationCode;
	}

	public void setActivationCode(String activationCode) {
		this.activationCode = activationCode;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "activation_date")
	public Date getActivationDate() {
		return activationDate;
	}

	public void setActivationDate(Date activationDate) {
		this.activationDate = activationDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "regist_date")
	public Date getRegistDate() {
		return registDate;
	}

	public void setRegistDate(Date registDate) {
		this.registDate = registDate;
	}

	@Column(name = "weight", length = 5)
	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	@Column(name = "height", length = 5)
	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	@Column(name = "schoolRecord", length = 1000)
	public String getSchoolRecord() {
		return schoolRecord;
	}

	public void setSchoolRecord(String schoolRecord) {
		this.schoolRecord = schoolRecord;
	}

	@Column(name = "training", length = 1000)
	public String getTraining() {
		return training;
	}

	public void setTraining(String training) {
		this.training = training;
	}

	@Column(name = "opus", length = 1000)
	public String getOpus() {
		return opus;
	}

	public void setOpus(String opus) {
		this.opus = opus;
	}

	@Column(name = "qq", length = 10)
	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	@Column(name = "music", length = 255)
	public String getMusic() {
		return music;
	}

	public void setMusic(String music) {
		this.music = music;
	}

	@Column(name = "pop_star", length = 255)
	public String getPopStar() {
		return popStar;
	}

	public void setPopStar(String popStar) {
		this.popStar = popStar;
	}

	@Column(name = "mei_mei_man")
	public int getMeiMeiMan() {
		return meiMeiMan;
	}

	public void setMeiMeiMan(int meiMeiMan) {
		this.meiMeiMan = meiMeiMan;
	}

	@Column(name = "mei_mei_member")
	public int getMeiMeiMember() {
		return meiMeiMember;
	}

	public void setMeiMeiMember(int meiMeiMember) {
		this.meiMeiMember = meiMeiMember;
	}

	@Column(name = "recommend")
	public int getRecommend() {
		return recommend;
	}

	public void setRecommend(int recommend) {
		this.recommend = recommend;
	}

	@Column(name = "active_code")
	public String getActiveCode() {
		return activeCode;
	}

	public void setActiveCode(String activeCode) {
		this.activeCode = activeCode;
	}

	@Column(name = "visit_count")
	public Long getVisitCount() {
		return visitCount;
	}

	public void setVisitCount(Long visitCount) {
		this.visitCount = visitCount;
	}

	public int getStauts() {
		return stauts;
	}

	public void setStauts(int stauts) {
		this.stauts = stauts;
	}

	@Column(name = "id_card")
	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	@Column(name = "job", length = 1000)
	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	@Column(name = "city", length = 50)
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Column(name = "school", length = 50)
	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	@Column(name = "avatar")
	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	@Transient
	public String getVocationChinese() {
		return vocationChinese;
	}

	public void setVocationChinese(String vocationChinese) {
		this.vocationChinese = vocationChinese;
	}

	@Transient
	public String getVocationEnglish() {
		return vocationEnglish;
	}

	public void setVocationEnglish(String vocationEnglish) {
		this.vocationEnglish = vocationEnglish;
	}

	@Transient
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

}